package com.xoa.controller.flowhook;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.xoa.util.CookiesUtil;
import com.xoa.util.common.session.SessionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xoa.model.workflow.FlowHookWithBLOBs;
import com.xoa.service.flowhook.FlowHookService;
import com.xoa.util.ToJson;
import com.xoa.util.dataSource.ContextHolder;

/**
 * 创建作者:   杨 胜
 * 创建日期:   2017-6-13 下午7:48:17
 * 类介绍  :    公文管理
 * 构造参数:
 */
@Controller
@RequestMapping("/flowhook")
public class FlowHookController {

    @Resource
    FlowHookService flowHookService;

    @RequestMapping("/saveHook")
    @ResponseBody
    public ToJson<FlowHookWithBLOBs> saveHook(FlowHookWithBLOBs flowHookWithBLOBs, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<FlowHookWithBLOBs> tojson = new ToJson<FlowHookWithBLOBs>();
        try{
            FlowHookWithBLOBs flowHookWithBLOBs1 = flowHookService.selectByOrderId(flowHookWithBLOBs);
            int result =0;
            FlowHookWithBLOBs flowHookWithBLOBs3 = flowHookService.selectByName(flowHookWithBLOBs);
            if(flowHookWithBLOBs3!=null&&flowHookWithBLOBs.getHname().equals(flowHookWithBLOBs3.getHname())){
                tojson.setMsg("保存失败，存在相同的名称!");
                tojson.setFlag(1);
            }else{
                if(flowHookWithBLOBs1!=null){
                    tojson.setMsg("保存失败，存在相同的排序号!");
                    tojson.setFlag(1);
                }else{
                    result = flowHookService.saveHook(flowHookWithBLOBs);
                    if (result == 1) {
                        tojson.setMsg("保存成功");
                        tojson.setFlag(0);
                    } else {
                        tojson.setMsg("保存失败");
                        tojson.setFlag(1);
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            tojson.setMsg("保存失败");
            tojson.setFlag(1);
        }

        return tojson;
    }

    @RequestMapping("/deleteHook")
    @ResponseBody
    public ToJson<FlowHookWithBLOBs> deleteHook(FlowHookWithBLOBs flowHookWithBLOBs, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<FlowHookWithBLOBs> tojson = new ToJson<FlowHookWithBLOBs>();
        try {
            int result = flowHookService.deleteHook(flowHookWithBLOBs);
            if (result == 1) {
                tojson.setMsg("删除成功");
                tojson.setFlag(0);
            } else {
                tojson.setMsg("删除失败");
                tojson.setFlag(1);
            }
        }catch (Exception e){
            e.printStackTrace();
            tojson.setMsg("删除失败");
            tojson.setFlag(1);
        }

        return tojson;
    }

    @RequestMapping("/updateHook")
    @ResponseBody
    public ToJson<FlowHookWithBLOBs> updateHook(FlowHookWithBLOBs flowHookWithBLOBs, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<FlowHookWithBLOBs> tojson = new ToJson<FlowHookWithBLOBs>();
        try{
            FlowHookWithBLOBs flowHookWithBLOBs2 = flowHookService.selectByOrderId(flowHookWithBLOBs);
            FlowHookWithBLOBs flowHookWithBLOBs3 = flowHookService.selectByName(flowHookWithBLOBs);
            if(flowHookWithBLOBs3!=null&&!flowHookWithBLOBs.getOrderId().equals(flowHookWithBLOBs3.getOrderId())){
                tojson.setMsg("修改失败，存在相同的名称");
                tojson.setFlag(1);
            }else{
                if(flowHookWithBLOBs2!=null&&!flowHookWithBLOBs.getOrderId().equals(flowHookWithBLOBs2.getOrderId())){
                    tojson.setMsg("修改失败，存在相同的排序号");
                    tojson.setFlag(1);
                }else{
                    FlowHookWithBLOBs flowHookWithBLOBs1 = flowHookService.selectByOrderId(flowHookWithBLOBs);
                    if (flowHookWithBLOBs1 != null) {
                        int result = flowHookService.updateHook(flowHookWithBLOBs);
                        if (result == 1) {
                            tojson.setMsg("修改成功");
                            tojson.setFlag(0);
                        } else {
                            tojson.setMsg("修改失败");
                            tojson.setFlag(1);
                        }
                    } else {
                        tojson.setMsg("修改失败，数据不存在");
                        tojson.setFlag(1);
                    }
                }
            }


        }catch (Exception e){
            e.printStackTrace();
        }
        return tojson;
    }

    @RequestMapping("/getHookById")
    @ResponseBody
    public ToJson<FlowHookWithBLOBs> getRuleById(FlowHookWithBLOBs flowHookWithBLOBs, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<FlowHookWithBLOBs> tojson = new ToJson<FlowHookWithBLOBs>();
        try{
            FlowHookWithBLOBs result = flowHookService.getHookById(flowHookWithBLOBs);
            tojson.setObject(result);
            tojson.setMsg("ok");
            tojson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            tojson.setMsg("err");
            tojson.setFlag(1);
        }
        return tojson;
    }

    @RequestMapping("/getHookAll")
    @ResponseBody
    public ToJson<FlowHookWithBLOBs> getHookAll(FlowHookWithBLOBs flowHookWithBLOBs, HttpServletRequest request) {
        Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
        String loginDateSouse = SessionUtils.getSessionInfo(request.getSession(), "loginDateSouse", String.class, redisSessionCookie);
        ContextHolder.setConsumerType("xoa" + loginDateSouse);
        ToJson<FlowHookWithBLOBs> tojson = new ToJson<FlowHookWithBLOBs>();
        try {
            List<FlowHookWithBLOBs> result = flowHookService.getHookAll(flowHookWithBLOBs);
            tojson.setObj(result);
            tojson.setMsg("ok");
            tojson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            tojson.setMsg("err");
            tojson.setFlag(1);
        }
        return tojson;
    }
    @RequestMapping("/queryFormId")
    @ResponseBody
    public ToJson<FlowHookWithBLOBs> queryFormId(String folwId){
     return  flowHookService.queryFormId(folwId);
    }


    /**
     *
     * 流程 门户上面的统计按照业务应用 查找接口和流程ID
     * @param request
     * @return
     */
    @RequestMapping("/queryCountDataByQuarter")
    @ResponseBody
    public ToJson queryCountDataByQuarter(HttpServletRequest request){
        return flowHookService.queryCountDataByQuarter(request);
    }
}